package com.org.sort;

import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ForkJoinTask;

@SuppressWarnings("serial")
public class ArrayConcurrentSortComparator<T> extends AbConcurrentArrySort<T> {
    private Comparator<? super T> comparator;

    private ArrayConcurrentSortComparator(T[] array, int began, int end, int threadloads,
            Comparator<? super T> comparator) {
        super(array, began, end, threadloads);
        this.comparator = comparator;
    }

    public ArrayConcurrentSortComparator(T[] array, int threadloads, Comparator<? super T> comparator) {
        super(array, threadloads);
        this.comparator = comparator;
    }

    public ArrayConcurrentSortComparator(T[] array, Comparator<? super T> comparator) {
        super(array);
        this.comparator = comparator;
    }

    @Override
    protected int compare(T t1, T t2) {
        return comparator.compare(t1, t2);
    }

    @Override
    protected ForkJoinTask<Void> invokeTaskLeft(int pos) {
        return new ArrayConcurrentSortComparator<>(array, began, pos - 1, threadloads, comparator);
    }

    @Override
    protected ForkJoinTask<Void> invokeTaskRight(int pos) {
        return new ArrayConcurrentSortComparator<>(array, pos + 1, end, threadloads, comparator);
    }

    @Override
    public void sortRange(int start, int end) {
        Arrays.sort(array, began, end, comparator);
    }
}
